﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.SqlServerDAL
{
    public class NewsSkinDAL : INewsSkins
    {
        private Builder<NewsSkinInfo> skinsDAL = new Builder<NewsSkinInfo>("NewsSkins", "Id");

        public NewsSkinDAL()
        {
            skinsDAL.CreateModel += new ReaderBind<NewsSkinInfo>(skinsDAL_CreateModel);
        }

        NewsSkinInfo skinsDAL_CreateModel(Hashtable reader)
        {
            NewsSkinInfo info = new NewsSkinInfo();

            if (reader.ContainsKey("Id"))
                info.Id = Convert.ToInt32(reader["Id"]);

            if (reader.ContainsKey("Skin"))
                info.Skin = reader["Skin"].ToString();

            if (reader.ContainsKey("Preview"))
                info.Preview = reader["Preview"].ToString();

            return info;
        }

        #region INewsSkins 成员

        public Lswweb.Model.NewsSkinInfo GetSkin(int categoryId)
        {
            if (categoryId <= 0)
                return null;

            string tableName = "NewsCategories,NewsSkins";
            string fields = "NewsSkins.*";
            string where = "NewsCategories.SkinId=NewsSkins.Id and NewsCategories.Id=@CategoryId";
            return skinsDAL.GetModel(tableName, fields, where, string.Empty,DataAccess.CreateParameter("CategoryId",categoryId));
        }

        public IList<Lswweb.Model.NewsSkinInfo> GetSkins()
        {
            string sql="select * from NewsSkins";
            return skinsDAL.GetModels(sql, null);
        }

        public bool UpdateCategorySkin(int categoryId, int skinId)
        {
            if (categoryId <= 0 || skinId <= 0)
                return false;

            string sql = "update NewsCategories set SkinId=" + skinId + " where Id=" + categoryId + ";";
            return skinsDAL.Update(sql, null);
        }

        #endregion
    }
}
